import { getArce } from '@/api/arce'
const state = {
  agData: [],
  nameData: []
}
const mutations = {
  setAg(state, data) {
    state.agData = data
    localStorage.setItem('agData', JSON.stringify(data))
  },
  setName(state, data) {
    state.nameData = data
    localStorage.setItem('namData', JSON.stringify(data))
  }
}
const actions = {
  async getAgName(store) {
    const nameArr = []
    const agentList = [{ value: 1, name: '' }]
    let arr = []
    // async() => {
    const res = await getArce()
    arr = res.data
    let count = 0
    // console.log(res.data)
    arr.forEach((item, index) => {
      if (!agentList.some((v, index) => {
        // console.log(v.name, item.agent.name)
        if (v.name === (item.agent && String(item.agent.name))) {
          count = index
          return v.name === (item.agent && String(item.agent.name))
        }
      })) {
        agentList.push({ value: 1, name: `${(item.agent && String(item.agent.name))}` })
      } else {
        agentList[count].value++
      }
    })
    agentList.splice(0, 1)
    agentList.forEach(v => {
      nameArr.push(v.name)
    })
    store.commit('setAg', agentList)
    store.commit('setName', nameArr)
  }

}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}

